package com.bdqn.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.crm.pojo.Pagee;
import com.bdqn.crm.pojo.SysRole;
import com.bdqn.crm.pojo.SysUser;
import com.bdqn.crm.service.SysRoleService;
import com.bdqn.crm.service.SysUserService;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class SysUserController {

    @Resource
    private SysUserService sysUserService;

    @Resource
    private SysRoleService sysRoleService;

    @RequestMapping("/doLogin")
    public String doLogin(Model model, HttpServletRequest request, String usrName, String usrPassword){
        System.out.println(usrName+","+usrPassword);
        SysUser sysUser = sysUserService.getlogin(usrName,usrPassword);
        if(sysUser !=null){
//            model.addAttribute("loginUser",sysUser);
            request.getSession().setAttribute("loginUser",sysUser);
            return "main";
        }else{
            model.addAttribute("message","登录失败，密码或用户错误");
//            request.setAttribute("message","登录失败，密码或用户错误");
        }

        return "login";
    }
    @RequestMapping(value = "/user/list")
    public String list(HttpSession session,Model model,@RequestParam(required = false) String usrName,
                       @RequestParam(required = false,defaultValue = "0") Long roleId,
                       @RequestParam(required = false,defaultValue = "0") int pageIndex){
        //数据回显
        model.addAttribute("usrName",usrName);
        model.addAttribute("roleId",roleId);

        //获取角色集合数据
        List<SysRole> list = sysRoleService.findAllRoles(null);
        model.addAttribute("roles",list);

        //页面查询过来的条件通过SysUser封装
        SysUser sysUser= new SysUser();
        sysUser.setRoleId(roleId);
        sysUser.setUsrName(usrName);

        //划分分页
        Pagee<SysUser> pagee = new Pagee();
        pagee.setTotalCount(sysUserService.getPageListTotal(sysUser));  //获取总记录数设置在分页中
        pagee.setCurrPageNo(pageIndex);  //当前页
        model.addAttribute("userPager",pagee);
        //设置分页信息
        List<SysUser> pageList = sysUserService.getPageList(sysUser,pagee);
        model.addAttribute("user",pageList);
        return "user/list";
    }

    /**
     * 跳转添加页面
     * @param model
     * @return
     */
    @GetMapping(value = "/user/add")
    public String add(Model model){
        return "user/add";
    }

    @PostMapping(value = "user/save")
    public String save(Model model,@RequestParam(required = false,defaultValue = "0")String usrName,
                       @RequestParam(required = false,defaultValue = "0")String usrPassword,
                       @RequestParam(required = false,defaultValue = "0")Long roleId,
                       @RequestParam(required = false,defaultValue = "0")Long usrFlag){
        //获取角色集合数据
        List<SysRole> list1 = sysRoleService.findAllRoles(null);
        model.addAttribute("roles",list1);

        return "user/add";
    }

}
